//! # Player 存储的信息类型
//!
//! # 示例
//!
//! ```
//! let msg = TrackMessage::new(0, 0, kind);
//! ```

use std::cmp::Ordering;

use midly::TrackEventKind;

/// 轨道消息
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrackMessage<'a> {
    /// 轨道号
    pub track: usize,
    /// 时间戳
    pub time: u32,
    /// 事件类型
    pub kind: TrackEventKind<'a>,
}

impl<'a> TrackMessage<'a> {
    /// 构造方法
    pub fn new(track: usize, time: u32, kind: TrackEventKind<'a>) -> TrackMessage<'a> {
        TrackMessage { track, time, kind }
    }
}

impl<'a> Ord for TrackMessage<'a> {
    /// 比较方法
    ///
    /// 1. 优先按照时间排序
    /// 2. 再按照轨道排序
    fn cmp(&self, other: &Self) -> Ordering {
        other
            .time
            .cmp(&self.time)
            .then_with(|| other.track.cmp(&self.track))
    }
}

impl<'a> PartialOrd for TrackMessage<'a> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
